import Vue from 'vue'

const language = localStorage.getItem('language')
// console.log(language);

function deepClone(vnodes, createElement) {
    if (vnodes.length < 1) return vnodes

    let dropdownListNodes = []
    const clonedVNodes = [];

    vnodes.forEach((vnode) => {
        const children = vnode.children.filter(child=> {
            if (!(child.tag ==='i' && child.data.staticClass.includes('iconfont')))
                return child
        })
        const cloned = createElement('dropdown-item', children)
        cloned.data.on = vnode.data.on;
        dropdownListNodes.push(cloned)
    })

    let tag = createElement('a', {
        attrs: {href: "javascript:void(0)"},
    }, [createElement('i', {attrs: {class: "iconfont zy-common-gengduo1"}}), createElement('span', {domProps: {innerText: language !== 'zh_CN' ? 'More' : '更多'}})])

    let dropdownList = createElement('dropdown-menu', {slot: "list"}, [dropdownListNodes])
    let dropdownHtml = createElement('dropdown', {
        props: {
            placement: "left-start", transfer: true, transferClassName: 'theme-dropdown', trigger: "click"
        }, attrs: {class: "ivu-dropdown"}
    }, [tag, dropdownList])

    clonedVNodes.push(dropdownHtml)

    return clonedVNodes;
}

Vue.component('autoPlaceChildren', {
    render(createElement) {

        let children = this.$slots.default.filter(item => {
            if (item.tag) return item;
        })
        const topThree = children.length < 4 ? children.splice(0, 3) : children.splice(0, 2)

        return createElement('div', [...topThree, ...deepClone(children, createElement)])
    }
})

